using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MWTools.Client.Common.Interfaces;
using System.Data.SqlServerCe;
using MWTools.Client.Common;
using System.Reflection;
using System.Data;
using System.IO;

namespace MWTools.Client.Dal
{
    public class OutlookSetupHelper
    {
        public static void CreateClientTablesFromType(Type tableName, string connection)
        {
            using (SqlCeConnection conn = new SqlCeConnection(string.Format("DATA SOURCE='{0}'", connection)))
            {
                SqlCeCommand cmd = new SqlCeCommand();
                cmd.Connection = conn;
                cmd.CommandText += DataMappingUtility.GenerateEntityTable(tableName, true);
                conn.Open();
                MWTools.Client.Common.Logger.DebugFormat(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "executing sql:{0}", cmd.CommandText);

                cmd.ExecuteNonQuery();
            }
        }

        public static void EnsureSettingsData()
        {
            if (!File.Exists(Constants.SETTINGS_STORE_PATH))
            {
                try
                {
                    Logger.DebugFormat("EnsureSettingsData", "file {0} does not exist, creating", Constants.SETTINGS_STORE_PATH);

                    DirectoryInfo di = new DirectoryInfo(Constants.SETTINGS_STORE_PATH).Parent;

                    if (!di.Exists)
                    {
                        di.Create();
                        Logger.DebugFormat("EnsureSettingsData", "creating dir at {0}", di.Name);
                    }


                    using (SqlCeEngine engine = new SqlCeEngine(string.Format("DATA SOURCE='{0}'", Constants.SETTINGS_STORE_PATH)))
                    {
                        Logger.DebugFormat("CreateOutlookTables", "Creating db at {0}", Constants.SETTINGS_STORE_PATH);

                        engine.CreateDatabase();
                    }

                    CreateClientTablesFromType(typeof(UserSetting), Constants.SETTINGS_STORE_PATH);
                    //CreateClientTablesFromType(typeof(Microsoft.Office.Interop.Outlook.MailItem), Constants.SETTINGS_STORE_PATH);

                    UserSetting setting = MWTools.Client.AppContext.GetDefaultSetting();
                    UserSettingDal.UserSettingInsert(setting);
                }
                catch (Exception ex)
                {
                    Logger.DebugFormat("OutlookSetupHelper.EnsureSettingsData", "invalid settings deteced deleting");
                    System.IO.File.Delete(Constants.SETTINGS_STORE_PATH);
                    EnsureSettingsData();
                }
                
            }
        }

        public static void CreateOutlookTables()
        {
            if (File.Exists(Constants.SETTINGS_STORE_PATH))
            {
                Logger.DebugFormat("CreateOutlookTables", "deleting existing db at {0}", Constants.SETTINGS_STORE_PATH);

                File.Delete(Constants.SETTINGS_STORE_PATH);
            }

            using (SqlCeEngine engine = new SqlCeEngine(string.Format("DATA SOURCE='{0}'", Constants.SETTINGS_STORE_PATH)))
            {
                Logger.DebugFormat("CreateOutlookTables", "Creating db at {0}", Constants.SETTINGS_STORE_PATH);

                engine.CreateDatabase();
            }

            CreateClientTablesFromType(typeof(UserSetting), Constants.SETTINGS_STORE_PATH);
            //CreateClientTablesFromType(typeof(AppointmentItem), Constants.SETTINGS_STORE_PATH);
            //CreateClientTablesFromType(typeof(ContactItem), Constants.SETTINGS_STORE_PATH);
            //CreateClientTablesFromType(typeof(EmailItem), Constants.SETTINGS_STORE_PATH);
            //CreateClientTablesFromType(typeof(SyncItem), Constants.SETTINGS_STORE_PATH);

        }
    }
}
